/*
 * @author Brandon Wong 
 */

package com.apps.services;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import com.apps.services.factory.RMPServiceFactory;
import com.apps.ubc.cc.model.RMPModel;
import com.apps.utils.RMPUtils;
import com.apps.utils.WrapperUtils;

public class RMPService {

	// private String tid =
	// "<td><a href=\"ShowRatings.jsp?tid=923805\">Voll, Kimberly</a></td>";

	private static String is_name_link = "<td><a href=\"ShowRatings.jsp?tid=<name>\">";
	private static String start = "<td><a href=\"ShowRatings.jsp?";
	private static String UNIVERSITY = "University of British Columbia - Vancouver";

	
	public RMPService(){
	}
	
	public List<RMPModel> getProfInfo(String name) {
		List<RMPModel> out = new ArrayList<RMPModel>();
		if(name == null || name.isEmpty())
			return out;
		String[] profnames = RMPUtils.getMultipleNames(name);
		for(int i = 0; i < profnames.length; i++){
			String p_last_name = RMPUtils.parseFirstLastName(profnames[i])[0];
			String p_first_name = RMPUtils.parseFirstLastName(profnames[i])[1];
		String result = "";
		p_last_name = p_last_name.toLowerCase();
		String tid = "";
		String search_name = "";
		boolean isFound=false;

		try {

			BufferedReader br = WrapperUtils
					.getURLContentReader(WrapperUtils.RMP_SEARCH_URL.replace(
							"<prof last name>", p_last_name));

			String current_line = br.readLine();

			while (current_line!=null&&!current_line.contains("<!-- Career Rookie form -->")) {
				if (current_line.contains(start)) {
					 //System.out.println("value is " + current_line);
					
					tid = WrapperUtils.stringInBwtn(
							"<a href=\"ShowRatings.jsp?tid=", "\"><",
							current_line);
					
					//System.out.println("tid is " + tid);
					current_line = br.readLine();
					while (current_line!=null&&!current_line.contains(start)) {
						// Loop until you see it.
						// System.out.println(current_line);
						// Just in case
					}
					
					String namer = is_name_link.replace("<name>", tid);
					//System.out.println(namer);
					//System.out.println(">>"+current_line);
					search_name = WrapperUtils.stringInBwtn(namer, "</a></td>", current_line);
					//System.out.println(is_name);
					current_line = br.readLine();
					while (current_line!=null&&!current_line.contains("<td><a href=")) {
						current_line = br.readLine();
						// Loop until you see it.
						// System.out.println(current_line);
						// Just in case
					}
					String school_name=WrapperUtils.stringInBwtn("\">", "</a></td>", current_line);
					//System.out.println("school "+school_name);
					
					
					String s_first = "";
					
					String[] fl = RMPUtils.parseFirstLastName(search_name);
					String s_last = fl[0];
					
					if(fl.length > 1)
						s_first = fl[1];
					 

						if (p_last_name.toLowerCase().equals(s_last.toLowerCase()) && s_first.toLowerCase().startsWith(p_first_name.toLowerCase()) && school_name.toLowerCase().equals(UNIVERSITY.toLowerCase())) {
							
								result = getOverallQuality(WrapperUtils.RMP_INFO_URL.replace("<tid>", tid));	
							//System.out.println("YOYOYO " + is_name);
							isFound=true;
							break;
						}
					
				}
				if(isFound==true){
					break;
				}
				current_line = br.readLine();
			}

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		RMPModel rm = new RMPModel (profnames[i], "not found", "not found"); 
		if(!result.equals(""))
			rm =new RMPModel (profnames[i], result, WrapperUtils.RMP_INFO_URL.replace("<tid>", tid));
		System.out.println(rm.toString());
		out.add(rm);
		}
		return out; // method stub
	}
	
	
	public String getOverallQuality(String url){
		BufferedReader br;
		try {
			br = WrapperUtils.getURLContentReader(url);
			String line = br.readLine();
			
			while(line != null && !line.contains("<div id=\"otherInfo\">")){
				//System.out.println("FINDING PROF INFOR: "+line);
				if(line.contains("<div id=\"scoreCard\">")){
					line=br.readLine();
					while(line!=null&&!line.contains("<li class=\"tTip\" id=\"quality")){
						
						line=br.readLine();
					}
					//System.out.println(line);
					String overall=WrapperUtils.stringInBwtn("<strong>","</strong>", line);
					return overall;
					
				}
				line = br.readLine();
			}
	
			
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "";//method stub
	
	}
	
	public static void main(String[] a){
		new RMPService().getProfInfo("Wohlstadter, Eric");
	}
	
}
